package tedu.rocketmq.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import tedu.rocketmq.service.HelloService;

import java.util.List;

@Slf4j
@RestController
public class HelloController {

    @Autowired
    RocketMQTemplate rocketMQTemplate;

    @GetMapping("/send")
    public String sayHi(String name){

        //1.准备一个message对象
        // MessageBuilder 工具类 -快捷构建消息对象
        // withPayload() 设置了消息的负载内容
        Message<String> message = MessageBuilder.withPayload(name).build();
        //2.根据需求发送消息(同步\异步\延迟)
        //rocketMQTemplate.syncSend("test-topic01",message);
        //同步延迟发送 timeout:发送消息的超时时间  delayLevel:4 - 30s后发送
        //rocketMQTemplate.syncSend("test-topic01",message,1000,4);
        //异步发送 回调函数
        rocketMQTemplate.asyncSend("test-topic01", message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("发送成功:{}",sendResult);
            }
            @Override
            public void onException(Throwable throwable) {
                log.info("发送失败:{}",throwable);
            }
        });
        log.info("发送消息成功:{}",name);
        return "SUCCESS..";
    }

/*
    @GetMapping("/get")
    public List<String> get(){
        List<String> receive = rocketMQTemplate.receive(String.class);
        return receive;
    }
*/


}







